package com.haizhi.mespush.job;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.haizhi.mespush.bean.Person;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HostMesPushJob implements Job {
    
    private static final int timeout = 10000;

    public static int index = 0;

    private static final String dingUrl = "https://oapi.dingtalk.com/robot/send?access_token=9f45cc1707bbe0a29482dda4528c2b665c188e6d121ba0de3e15671eb7c2229f";

    private static String[] personList = new String[]{
            "86-17863671679",
            "86-17853687976",
            "86-15610591153"
    };


    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        try {

            if(index == personList.length){
                index = 0;
            }
            boolean isAtAll = false;
            //通知具体人的手机号码列表
            List<String> mobileList = new ArrayList<String>();
            mobileList.add(personList[index++]);
            //钉钉机器人消息内容
            String content = "大家好!本周主持周会的是";
            //组装请求内容
            String reqStr = buildReqStr(content, isAtAll, mobileList);
            //推送消息（http请求）
            String body = HttpRequest.post(dingUrl).body(reqStr).timeout(timeout).execute().body();
            mobileList.clear();
            System.out.println("主持->" + body);

        }catch (Exception e){
            e.printStackTrace();
            System.out.printf("error"+e.getMessage());
        }
    }

    private static String buildReqStr(String content, boolean isAtAll, List<String> mobileList) {
        //消息内容
        Map<String, String> contentMap = new HashMap<String, String>();
        contentMap.put("content", content);

        //通知人
        Map<String, Object> atMap = new HashMap<String, Object>();
        //1.是否通知所有人
        atMap.put("isAtAll", isAtAll);
        //2.通知具体人的手机号码列表
        atMap.put("atMobiles", mobileList);

        Map<String, Object> reqMap = new HashMap<String, Object>();
        reqMap.put("msgtype", "text");
        reqMap.put("text", contentMap);
        reqMap.put("at", atMap);
        return JSONUtil.toJsonStr(reqMap);
    }
    
}
